/*
 * (C) Copyright 2006-2010 Nuxeo SAS (http://nuxeo.com/) and contributors.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 2.1 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * Contributors:
 *     bstefanescu
 */
package org.nuxeo.gwt.habyt.templates.client;

import org.nuxeo.gwt.habyt.invocation.client.ErrorWidget;
import org.nuxeo.gwt.habyt.templates.client.bindings.Helper;

import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.Node;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.Widget;

/**
 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
 * 
 */
public abstract class WidgetFactory {

    protected String id;

    public WidgetFactory(String id) {
        this.id = id;
    }

    public String getId() {
        return id;
    }

    public Widget getWidget(Element element) {
        try {
            Widget w = createWidget(element);
            loadAttributes(w, element);
            Helper.bindWidget(w);
            return w;
        } catch (Throwable t) {
            return new ErrorWidget("Failed to bind element "
                    + element.getTagName(), t);
        }
    }

    protected abstract Widget createWidget(Element element);

    protected void loadAttributes(Widget w, Element element) {
        String id = Helper.getIdAttribute(element);
        String width = Helper.getWidthAttribute(element);
        String height = Helper.getHeightAttribute(element);
        String style = Helper.getStyleAttribute(element);
        String className = Helper.getClassAttribute(element);
        if (id != null) {
            w.getElement().setId(id);
        }
        if (width != null) {
            DOM.setStyleAttribute(w.getElement(), "width", width);
        }
        if (height != null) {
            DOM.setStyleAttribute(w.getElement(), "height", height);
        }
        if (style != null) { // TODO append style?
            Helper.setSyleAttribute(w.getElement(), style);
        }
        if (className != null) {
            Helper.addClassAttribute(w.getElement(), className);
        }
    }

    protected void copyAttributes(Element src, Element dst, String... attrs) {
        for (String attr : attrs) {
            String v = Helper.getAttribute(src, attr);
            if (v != null) {
                dst.setAttribute(attr, v);
            }
        }
    }

    protected void importContent(Widget w, Element element) {
        Element targetEl = w.getElement();
        Node el = element.getFirstChild();
        while (el != null) {
            Node elToImport = el;
            el = el.getNextSibling();
            targetEl.appendChild(elToImport);
        }
    }

}
